#include('./header.html',{active:'category', title:'分类管理'})
<div class="row">
    <div class="col-sm-12">
        <h4 class="page-title">分类/标签管理</h4>
    </div>
    <div class="col-md-6">
        <div class="panel panel-color panel-primary">
            <div class="panel-heading">
                <h1 class="panel-title">分类列表</h1>
            </div>
            <div class="panel-body">
                #for(c : categories)
                <div class="btn-group m-b-10">
                    #if(c.name == '默认分类')
                    <button type="button" class="btn btn-${rand_color()} waves-effect waves-light">${c.name}
                        (${c.count})
                    </button>
                    #else
                    <button type="button" class="btn btn-${rand_color()} dropdown-toggle waves-effect waves-light"
                            data-toggle="dropdown" aria-expanded="false">${c.name} (${c.count}) <span
                            class="caret"></span></button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="javascript:void(0)" mid="${c.mid}" cname="${c.name}" class="edit-category">修改</a>
                        </li>
                        <li><a href="javascript:void(0)" mid="${c.mid}" class="del-category">删除</a></li>
                    </ul>
                    #end
                </div>
                #end
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="panel  panel-pink">
            <div class="panel-heading">
                <h1 class="panel-title">标签列表</h1>
            </div>
            <div class="panel-body">
                #for(c : tags)
                <div class="btn-group m-b-10">
                    <button type="button" class="btn btn-${rand_color()} dropdown-toggle waves-effect waves-light"
                            data-toggle="dropdown" aria-expanded="false">${c.name} (${c.count}) <span
                            class="caret"></span></button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="javascript:void(0)" mid="${c.mid}" class="del-category">删除</a></li>
                    </ul>
                </div>
                #end
            </div>
        </div>
    </div>

    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <form id="cform" class="form-inline" role="form">
                    <input type="hidden" id="mid"/>
                    <div class="form-group">
                        <input type="text" class="form-control" id="cname" placeholder="请输入分类名称">
                    </div>
                    <button id="save-category-btn" type="button"
                            class="btn btn-success waves-effect waves-light m-l-10">保存分类
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
#include('./footer.html')
<script type="text/javascript">
    $('#save-category-btn').click(function () {
        var cname = $('#cform #cname').val();
        var mid = $('#cform #mid').val();
        if (cname && cname != '') {
            $.post('/admin/category/save', {mid: mid, cname: cname}, function (result) {
                $('#cform #mid').val('');
                $('#cform #cname').val('');
                if (result && result.success) {
                    swal('提示信息', '分类保存成功', 'success');
                    setTimeout(function () {
                        window.location.reload();
                    }, 2000);
                } else {
                    swal("提示消息", result.msg, 'error');
                }
            });
        }
    });

    $('.edit-category').click(function () {
        var mid = $(this).attr('mid');
        var cname = $(this).attr('cname');
        $('#cform #mid').val(mid);
        $('#cform #cname').val(cname);
    });

    $('.del-category').click(function () {
        var mid = $(this).attr('mid');
        swal({
            title: '警告信息',
            text: "确定删除该项吗？",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        }).then(function () {
            $.post('/admin/category/delete', {mid: mid}, function (result) {
                if (result && result.success) {
                    swal('提示信息', '删除成功', 'success');
                    setTimeout(function () {
                        window.location.reload();
                    }, 2000);
                } else {
                    swal("提示消息", result.msg, 'error');
                }
            });
        });
    });

</script>
</body>
</html>